//    Copyright (C) Kherty.  All rights reserved.
using System;
using OpenLS.Core;

namespace OpenLS.Spreadsheet
{
    static class PercentManager
    {
        
    }
    class PercentValue
    {
        public static PercentValue TryParse(string stringValue)
        {
            string s2 = stringValue.Trim();
            if (s2.EndsWith("%") && s2.Length > 1)
            {
                s2 = s2.Substring(0, s2.Length - 1);
                double d;
                if (double.TryParse(s2, out d))
                {
                    double value = d / 100d;
                    Format format;
                    if (Math.Floor(d) != d)
                        format = OpenLS.Core.Formats.DecimalPercent;
                    else
                        format = OpenLS.Core.Formats.Percent;
                    return new PercentValue(value, format);
                }
            }
            return null;

        }
        public PercentValue(double value, Format format)
        {
            _value = value;
            _format = format;
        }
        public override string ToString()
        {
            if (Format != null)
                return Format.GetFormattedValue(Value).ToString();
            return (Value * 100) + "%";
        }
        private readonly double _value;
        public double Value
        {
            get { return _value; }
        }
        private Format _format;

        public Format Format
        {
            get { return _format; }
            set { _format = value; }
        }
	
	
    }
}
